<template>
  <div v-loading="delLoading">
    <Search :search="search" :reset="reset">
      <template v-slot:body>
        <el-form-item label="盘点单号" prop="docno">
          <el-input v-model="query.docno" name="docno" clearable />
        </el-form-item>
        <el-form-item label="盘点方案编码" prop="plan_no">
          <el-input v-model="query.plan_no" name="plan_no" clearable />
        </el-form-item>
        <el-form-item label="库存组织" prop="org_code">
          <el-input v-model="query.org_code" name="org_code" clearable />
        </el-form-item>
        <el-form-item label="单据状态" prop="statusArr">
          <!-- <el-input v-model="query.status" name="status" clearable /> -->
          <el-select v-model="query.statusArr" multiple placeholder="选择单据状态" style="width: 240px" clearable>
            <el-option v-for="item in DocStatusArr" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
      </template>
    </Search>
    <div class="table-default">
      <!-- <Operate :show="open" /> -->
      <el-table :data="tableData" class="mt-3" v-loading="loading" border>
        <el-table-column prop="docno" label="盘点单号" />
        <el-table-column prop="plan.plan_no" label="盘点方案编码" width="200" />
        <el-table-column prop="plan.plan_name" label="盘点方案名称" width="150" />
        <el-table-column prop="location" label="库位代码" />
        <el-table-column prop="org_code" label="库存组织" />
        <el-table-column label="单据状态" prop="status" width="130">
          <template #default="scope">
            <el-tag class="ml-2" type="primary" v-if="scope.row.status == 0">{{ DocStatus[scope.row.status] }}</el-tag>
            <el-tag class="ml-2" type="warning" v-if="scope.row.status == 10">{{ DocStatus[scope.row.status] }}</el-tag>
            <el-tag class="ml-2" type="success" v-if="scope.row.status == 20">{{ DocStatus[scope.row.status] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="创建时间" />
        <el-table-column label="操作" width="210">
          <template #default="scope">
            <router-link :to="{ path: '/inventory/inventoryOrderDetail', query: { id: `${scope.row.id}` } }" style="margin-left: 12px">
              <Show />
            </router-link>
            <el-button type="warning" size="small" style="margin-left: 12px" @click="approveHandler(scope.row.id)" v-if="scope.row.status != '1'" v-loading.fullscreen.lock="fullscreenLoading"
              >审核</el-button
            >
            <!-- <Update @click="open(scope.row.id)" /> -->
            <Destroy @click="destroy(api, scope.row.id)" />
          </template>
        </el-table-column>
      </el-table>
      <Paginate />
    </div>

    <Dialog v-model="visible" :title="title" destroy-on-close>
      <Create @close="close(reset)" :primary="id" :api="api" />
    </Dialog>
  </div>
</template>

<script lang="ts" setup>
import { computed, onMounted, ref, reactive, watch } from 'vue'
import Create from './create.vue'
import { useGetList } from '/admin/composables/curd/useGetList'
import { useDestroy } from '/admin/composables/curd/useDestroy'
import { useOpen } from '/admin/composables/curd/useOpen'
import http from '/admin/support/http'
import { ElMessage } from 'element-plus'
import { setTableHeight } from '/admin/utils/setHeight.js'

const api = 'inventory/inventory/order/list'

const { data, query, search, reset, loading } = useGetList(api)
const { destroy, deleted,delLoading } = useDestroy()
const { open, close, title, visible, id } = useOpen()

const tableData = computed(() => data.value?.data)
const fullscreenLoading = ref(false)

const approveHandler = id => {
  // editClick()

  fullscreenLoading.value = true

  http
    .put(`/takestock/interface/${id}`)
    .then(r => {
      fullscreenLoading.value = false

      ElMessage({
        message: r.data.message,
        type: 'success',
      })
      search()
    })
    .catch(error => {
      fullscreenLoading.value = false
    })
}

let DocStatus = reactive({})
// let DocStatusList = ref([])
let DocStatusArr = ref([])

let getDocStatus = () => {
  http.get(`options/InventoryOrderStatus`).then(r => {
    DocStatusArr.value = r.data.data
    r.data.data.forEach((item, index) => {
      DocStatus[item.value] = item.label
    })
    console.log('盘点单据状态', DocStatus)
  })
}

getDocStatus()

watch(
  () => query.value.statusArr,
  (newValue, oldValue) => {
    console.log('监听状态数组', newValue)
    query.value.status = newValue.toString()
  },
)

onMounted(() => {
  search()
  deleted(reset)
  setTableHeight('.table-default')
})
</script>

<style lang="css">
.el-table {
  height: 87%;
}
</style>
